package middleware

import (
	"saas-app/library/response"
	"strings"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func MiddlewareErrorHandler(r *ghttp.Request) {
	r.Middleware.Next()
	if err := r.GetError(); err != nil {
		errStr := err.Error()
		if strings.Contains(err.Error(), "NOWRITE::") {
			errStr = errStr[9:]
		} else {
			// 记录到自定义错误日志文件
			g.Log().Error(err)
		}
		r.Response.WriteStatus(200)
		//返回固定的友好信息
		r.Response.ClearBuffer()
		response.JsonExit(r, 1, errStr)
	}
}
